﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Topshelf;

namespace HttpForwardServer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string serviceInstallName = ConfigurationManager.AppSettings["serviceInstallName"]?.ToString();
                string serviceDisplayName = ConfigurationManager.AppSettings["serviceDisplayName"]?.ToString();
                string serviceDescription = ConfigurationManager.AppSettings["serviceDescription"]?.ToString();

                // 配置和运行宿主服务
                HostFactory.Run(x =>
                {
                    x.Service<Startup>(s =>
                    {
                        // 指定服务类型。这里设置为 Service
                        s.ConstructUsing(name => new Startup());
                        // 当服务启动后执行什么
                        s.WhenStarted(tc => tc.Start());
                        // 当服务停止后执行什么
                        s.WhenStopped(tc => tc.Stop());
                    });

                    // 服务用本地系统账号来运行
                    x.RunAsLocalSystem();
                    // 服务描述信息
                    x.SetDescription(serviceDescription);
                    // 服务显示名称
                    x.SetDisplayName(serviceDisplayName);
                    // 服务名称
                    x.SetServiceName(serviceInstallName);
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
